package com.tianji.aigc.tools;

import com.tianji.aigc.constant.ToolConstant;
import com.tianji.aigc.entity.CourseInfo;
import com.tianji.aigc.tools.result.ToolResultHolder;
import com.tianji.api.client.course.CourseClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 课程工具类
 *
 * @Author mr.wu
 * @Date 2025-8-19 9:58
 */
@Slf4j
@Component
public class CourseTool {

    @Autowired
    private CourseClient courseClient;


    @Tool(description = ToolConstant.Tools.QUERY_COURSE_BY_ID)
    public CourseInfo queryCourseById(@ToolParam(description = ToolConstant.Tool_Param.COURSE_ID) Long id
            , ToolContext toolContext) {
        log.info("[工具调用]根据课程ID查询课程详情，课程ID：{}", id);

        //封装返回结果对象
        CourseInfo courseInfo = CourseInfo.of(courseClient.baseInfo(id, true));

        //从ToolContext中获取传递过来的请求id
        String requestId = (String) toolContext.getContext().get(ToolConstant.REQUEST_ID);

        //封装field
        String field = "courseInfo_" + id;

        //将数据存放到结果保持器中，方便将来流水数据最后获取
        ToolResultHolder.put(requestId, field, courseInfo);
        return courseInfo;
    }
}